package de.htwg_konstanz.infen.aufg1.main;

import java.util.ArrayList;
import java.util.List;

import de.htwg_konstanz.infen.aufg1.inputXML.jaxbGenerated.AlternativeKey;
import de.htwg_konstanz.infen.aufg1.inputXML.jaxbGenerated.Attribute;
import de.htwg_konstanz.infen.aufg1.inputXML.jaxbGenerated.EntityTyp;
import de.htwg_konstanz.infen.aufg1.inputXML.jaxbGenerated.ErModel;
import de.htwg_konstanz.infen.aufg1.inputXML.jaxbGenerated.FunctionalDependencies;
import de.htwg_konstanz.infen.aufg1.inputXML.jaxbGenerated.RelationshipType;

/**
 * Date: 29.11.2010
 * Course: Information Engineering
 * Team: Valentin Durst, Tobias Kutter and Stefan Bernhard
 * 
 * Description:
 * The InputInfo class provides the information the MainClass and the Output helper 
 * needs to create their stuff without knowing about the XML input file.
 **/

public class InputInfos 
{
	private ErModel theErModel;
	// The ErModel is the class structure we recive from the input XML file
	// through JAXB
	public InputInfos(ErModel inErMod)
	{
		theErModel = inErMod;
	}
	
	// Returns a specific attribute from an entity type, most important is that
	// the other attributes are returned too. Like isRequired or Type.
	// This is needed if we have only the attribute name and want the whole informations
	// about it.
	public Attribute getAttibute(String entityName, String attrName)
	{
		EntityTyp et = getEntity(entityName);
		List<Attribute> atsList = et.getAttributes().getAttribute();
		for(Attribute ats : atsList)
		{
			if(ats.getName().equalsIgnoreCase(attrName))
				return ats;
		}
		for(Attribute ats : atsList)
		{
			String asd = ats.getName();
			if(asd.contains("_"))
				asd = asd.split("_")[1];
			if(asd.equalsIgnoreCase(attrName))
				return ats;
		}
		return null;
		
	}
	
	// Returns the entity for a given name.
	public EntityTyp getEntity(String name)
	{
		for(EntityTyp et : theErModel.getEntityTypes().getEntityTyp())
		{
			if(et.getName().equals(name))
				return et;
		}
		return null;
	}
	
	// get the primary key attributes from an entity.
	public List<Attribute> getPrimaryKey(String entityName)
	{
		return getEntity(entityName).getPrimaryKey().getAttribute();
	}
	
	// get the alternative key attributes from an entity
	public List<Attribute> getAlternativeKeys(String entityName)
	{
		List<Attribute> aks = new ArrayList<Attribute>();
		List<AlternativeKey> altkeys = null;
		if(getEntity(entityName).getAlternativeKeys() != null){
			altkeys = getEntity(entityName).getAlternativeKeys().getAlternativeKey();
		}
		if(altkeys != null){
			for(AlternativeKey ak : altkeys)
			{
				aks.addAll(ak.getAttribute());
			}
			return aks;
		}else{
			return null;
		}
	}
	
	// see name
	public List<EntityTyp> getAllEntities()
	{
		return theErModel.getEntityTypes().getEntityTyp();
	}
	
	// Returns the Relationship with the given name.
	public RelationshipType getRelationshipType(String name)
	{
		for(RelationshipType rt : theErModel.getRelationshipTypes().getRelationshipType())
		{
			if(rt.getName().equals(name))
				return rt;
		}
		return null;		
	}
	
	// see name
	public List<RelationshipType> getAllRelationships()
	{
		return theErModel.getRelationshipTypes().getRelationshipType();
	}

	// see name
	public FunctionalDependencies getAllFunctionalDependeciDependencies()
	{
		return theErModel.getFunctionalDependencies();
	}
}
